package com.dd.cloud.auth.service;

import com.dd.cloud.auth.config.LoginUser;
import com.dd.cloud.auth.entity.AdminRole;
import com.dd.cloud.auth.entity.RoleMenu;
import com.dd.cloud.common.constants.HealthConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.JsonUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.LoginInfoTransfer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 用户登录流程会走到这里
 * @Author: czg
 * @Date: 2023/5/30 10:46
 * @Version: 1.0.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    IAdminPowerService adminPowerService;
    @Autowired
    IUserToRoleService userToRoleService;
    @Autowired
    IRoleMenuService roleMenuService;
    @Autowired
    IAdminRoleService adminRoleService;

    /**
     * @description 此处功能 通过用户信息查出用户的权限和可用菜单及url
     * @param s: 登录的用户信息,json字符串
     * @return UserDetails
     * @author czg
     * @date 2023/6/8 14:55
    */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        try {
            if (s == null || StringUtils.isBlank(s)) {
                throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
            }
            s = URLDecoder.decode(s, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        LoginInfo loginInfo = JsonUtils.strToObject(s, LoginInfo.class);
        if (loginInfo == null || loginInfo == null) {
            throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
        }
        AdminRole roleByUser = adminRoleService.getAdminRoleByUser(loginInfo);
        //Integer roleId = userToRoleService.getRoleIdByUser(loginInfo);
        List<RoleMenu> roleMenus = roleMenuService.queryMenuByUserId(roleByUser.getId());
        List<String> collect = new ArrayList<>();
        //获取用户权限
        for (RoleMenu menu:
             roleMenus) {
            if (!StringUtils.isBlank(menu.getServerUrl())){
                if (menu.getServerUrl().indexOf(",")>=0){
                    collect.addAll( Arrays.asList(menu.getServerUrl().split(",")));
                } else {
                    collect.add(menu.getServerUrl());
                }
            }
        }

        //List<RoleMenu> roleMenuByType = adminPowerService.getRoleMenuByType(loginInfo.getType());
        //List<RoleMenu> auth = adminPowerService.queryUserMenus(loginInfo.getType(), loginInfo.getId());
        //List<String> collect = roleMenus.stream().map(RoleMenu::getServerUrl).collect(Collectors.toList());
        collect.removeIf(Objects::isNull);
        loginInfo.setRoleId(roleByUser.getId());
        return new LoginUser(loginInfo,roleByUser, collect);
    }
}
